import {
    Card,
    CardArr,
    CardJokerType,
    EntiretyCard,
    cardColor,
    cardSize,
} from "./types";

export class Deck {
    cards: Card[] = [];
    constructor(cards?: Card[]) {
        if (cards) {
            this.cards = cards;
        } else {
            this.init();
        }
    }
    private init() {
        const cardSizes = Object.values(cardSize);
        const cardColors = Object.values(cardColor);
        function getString() {
            return this.color + " " + this.size;
        }
        for (const size of cardSizes) {
            for (const color of cardColors) {
                this.cards.push(<Card>{
                    color: color,
                    size: size,
                    getString,
                });
            }
        }
        const smallJoker = {
            type: CardJokerType.small,
            getString() {
                return "small Joker";
            },
        };
        const bigJoker = {
            type: CardJokerType.big,
            getString() {
                return "big Joker";
            },
        };
        this.cards.push(smallJoker);
        this.cards.push(bigJoker);
    }
    /**
     * 打印当前牌组
     * @param cardPaid
     */
    public printCardPaid(): void {
        let result: string = "";
        this.cards.forEach((card, index) => {
            result += card.getString() + "\t";
            if ((index + 1) % 6 === 0) {
                console.log(result);
                result = "";
            }
        });
        if (result) {
            console.log(result);
        }
    }
    public shuffle() {
        this.cards.sort((a, b) => {
            return Math.random() - 0.5;
        });
    }
    public deal(): EntiretyCard {
        const player1 = this.getCard(17);
        const player2 = this.getCard(17);
        const player3 = this.getCard(17);
        const remain = this.getCard(3);
        return {
            player1,
            player2,
            player3,
            remain,
        };
    }
    private getCard(n: number) {
        return new Deck(this.cards.splice(0, n));
    }
}
